package com.sweet.thread.unsafe;

public class UnsafeBank {
    public static void main(String[] args) {
        // 账户
        Account ac = new Account("结婚基金", 100);

        DrawMoney me = new DrawMoney(ac, 50, "我");
        DrawMoney gf = new DrawMoney(ac, 70, "Girlfriend");

        new Thread(gf).start();
        new Thread(me).start();
    }
}

// 账户
class Account {
    private String cardName; // 卡名
    private Integer money; // 余额

    public Account(String cardName, Integer money) {
        this.cardName = cardName;
        this.money = money;
    }

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }

    public Integer getMoney() {
        return money;
    }

    public void setMoney(Integer money) {
        this.money = money;
    }
}

// 银行取钱
class DrawMoney implements Runnable {
    Account ac; // 账户
    int drawingMoney; // 取多少钱
    String name;

    @Override
    public void run() {
        // synchronized (ac) {
            if (ac.getMoney() - drawingMoney <= 0) {
                System.out.println(name + "卡上余额不足～");
                return;
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            ac.setMoney(ac.getMoney() - drawingMoney);
            System.out.println(ac.getCardName() + "卡上余额为：" + ac.getMoney());
            System.out.println(name + "手中的钱：" + drawingMoney);
        // }
    }

    public DrawMoney(Account ac, int drawingMoney, String name) {
        this.ac = ac;
        this.drawingMoney = drawingMoney;
        this.name = name;
    }
}